package dpc.service.fop.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.ejb.HibernateEntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import cn.sh.cares.dao.orm.hibernate.DefaultHibernateOrmExecution;
import cn.sh.cares.dao.orm.hibernate.query.SafeDetachedCriteria;
import cn.sh.cares.dao.orm.hibernate.query.SafeRestrictions;

import dpc.entity.FlightLeg;
import dpc.entity.FlightOperation;
import dpc.entity.Synchronize;

public class FOPServiceImpl {

	@Autowired
	DefaultHibernateOrmExecution entityManager;
	
	public void synchronize() {
		SafeDetachedCriteria criteria = SafeDetachedCriteria.forClass(Synchronize.class);
		criteria.addOrder(Order.desc("syncId"));
		Synchronize synchronize = entityManager.queryForUniqueObjectByCriteria(Synchronize.class, criteria);

		// 获取航班数据
		List<FlightLeg> flightLegs = getFlightsFromFois(synchronize.getRemoteTime());
		
		// 同步数据
		for (FlightLeg flightLeg : flightLegs) {
			
		}
	}
	
	public void synchronize(FlightLeg flightLeg) {
		// 获取航班运行数据
		SafeDetachedCriteria criteria = SafeDetachedCriteria.forClass(FlightOperation.class);
		criteria.add(SafeRestrictions.equal("legId", flightLeg.getLegId()));
		FlightOperation fop = entityManager.queryForUniqueObjectByCriteria(FlightOperation.class, criteria);
		
		if (fop == null) {
			// insert
		} else {
			// update
		}
	}
	
	/**
	 * 获取现场保障系统的航班数据
	 * @param lastLoadTime 最后一次加载航班数据的时间
	 * @return 
	 */
	public List<FlightLeg> getFlightsFromFois(Date lastLoadTime) {
		SafeDetachedCriteria criteria = SafeDetachedCriteria.forClass(FlightLeg.class);
		criteria.add(SafeRestrictions.between("flightDate", 
				DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.DATE), -2),
				DateUtils.addDays(DateUtils.truncate(new Date(), Calendar.DATE), 6)));
		if (lastLoadTime != null) {
			criteria.add(SafeRestrictions.greaterThan("lastUpdateTime", lastLoadTime));	
		}
		return entityManager.queryForListByCriteria(FlightLeg.class, criteria);
	}
	
	
	public void validate(FlightLeg flightLeg) {
		if (StringUtils.isEmpty(flightLeg.getFlightDate())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getAirlineCode())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getFlightNo())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getSchdlDepAirport())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getSchdlArrAirport())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getActype())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getStd())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getSta())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getStatus())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getAgent())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getFltProp())) {
			// ERROR
		}
		if (StringUtils.isEmpty(flightLeg.getIntlFlag())) {
			// ERROR
		}
		
		
		// 实际出发缺失
		// 出发时间 > 到达时间
		
		// 关舱门缺失
		// 关舱门时间 > 实际出发

		
		
	}
}
